package com.jsh.erp.service.invoice;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.service.ICommonQuery;
import com.jsh.erp.service.material.MaterialResource;
import com.jsh.erp.service.material.MaterialService;
import com.jsh.erp.utils.Constants;
import com.jsh.erp.utils.QueryUtils;
import com.jsh.erp.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Yik
 */
@Service(value = "invoice_component")
@InvoiceResource
public class InvoiceComponent implements ICommonQuery {

    @Resource
    private InvoiceService invoiceService;

    @Override
    public Object selectOne(Long id) throws Exception {
        return invoiceService.getInvoiceById(id);
    }

    @Override
    public List<?> select(Map<String, String> map)throws Exception {
        return getMaterialList(map);
    }

    private Map<String,String> integration(String search){
        Map<String,String> mapByVal = new HashMap<>(6);
        mapByVal.put("beginTime",StringUtil.getInfo(search, "beginTime"));
        mapByVal.put("endTime",StringUtil.getInfo(search, "endTime"));
        mapByVal.put("invoiceNumber",StringUtil.getInfo(search, "invoiceNumber"));
        mapByVal.put("invoiceDate",StringUtil.getInfo(search, "invoiceDate"));
        mapByVal.put("invoiceType",StringUtil.getInfo(search, "invoiceType"));
        return mapByVal;
    }

    private List<?> getMaterialList(Map<String, String> map) throws Exception{
        Map<String,String> mapByVal = this.integration(map.get(Constants.SEARCH));
       return invoiceService.getAllInvoices(mapByVal);
    }

    @Override
    public Long counts(Map<String, String> map)throws Exception {
        Map<String,String> mapByVal = this.integration(map.get(Constants.SEARCH));
        return invoiceService.countInvoicesByConditions(mapByVal);
    }

    @Override
    public int insert(JSONObject obj, HttpServletRequest request) throws Exception{
        return invoiceService.addInvoice(obj, request);
    }

    @Override
    public int update(JSONObject obj, HttpServletRequest request)throws Exception {
//        return materialService.updateMaterial(obj, request);
        return 1;
    }


    @Override
    public int delete(Long id, HttpServletRequest request)throws Exception {
//        return materialService.deleteMaterial(id, request);
        return 1;
    }

    @Override
    public int deleteBatch(String ids, HttpServletRequest request)throws Exception {
//        return materialService.batchDeleteMaterial(ids, request);
        return 1;
    }

    @Override
    public int checkIsNameExist(Long id, String name)throws Exception {
//        return materialService.checkIsNameExist(id, name);
        return 1;
    }
}
